{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "定义网络架构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test(\n",
      "  (model1): Sequential(\n",
      "    (0): Conv2d(3, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))\n",
      "    (1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
      "    (2): Conv2d(32, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))\n",
      "    (3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
      "    (4): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))\n",
      "    (5): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
      "    (6): Flatten(start_dim=1, end_dim=-1)\n",
      "    (7): Linear(in_features=1024, out_features=64, bias=True)\n",
      "    (8): Linear(in_features=64, out_features=10, bias=True)\n",
      "  )\n",
      ")\n",
      "torch.Size([64, 10])\n",
      "tensor([[-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128],\n",
      "        [-0.0192, -0.0846, -0.2341,  0.0094, -0.0428,  0.0647,  0.0067,  0.0531,\n",
      "          0.0632, -0.0128]], grad_fn=<AddmmBackward0>)\n"
     ]
    }
   ],
   "source": [
    "# 网络模型\n",
    "import torch\n",
    "from torch import nn\n",
    "\n",
    "\n",
    "class Test(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.model1 = nn.Sequential(\n",
    "            nn.Conv2d(3, 32, 5, padding=2),\n",
    "            nn.MaxPool2d(2),\n",
    "            nn.Conv2d(32, 32, 5, padding=2),\n",
    "            nn.MaxPool2d(2),\n",
    "            nn.Conv2d(32, 64, 5, padding=2),\n",
    "            nn.MaxPool2d(2),\n",
    "            nn.Flatten(),\n",
    "            nn.Linear(1024, 64),\n",
    "            nn.Linear(64, 10),\n",
    "        )\n",
    "\n",
    "    def forward(self, x):\n",
    "        x = self.model1(x)\n",
    "        return x\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    test = Test()\n",
    "    print(test)\n",
    "    x = torch.ones((64, 3, 32, 32))\n",
    "    output = test(x)\n",
    "    print(output.shape)\n",
    "    print(output)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "# import torch.cuda\n",
    "import torchvision\n",
    "from module import *\n",
    "# import torch.nn as nn\n",
    "from torch.utils.data import DataLoader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to ./CIFAR10_dataset\\cifar-10-python.tar.gz\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 170498071/170498071 [00:34<00:00, 4887888.89it/s] \n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting ./CIFAR10_dataset\\cifar-10-python.tar.gz to ./CIFAR10_dataset\n",
      "Files already downloaded and verified\n",
      "训练集的长度为：50000\n",
      "测试集的长度为：10000\n"
     ]
    }
   ],
   "source": [
    "# 准备数据集\n",
    "train_dataset = torchvision.datasets.CIFAR10('./CIFAR10_dataset', train=True,\n",
    "                                            transform=torchvision.transforms.ToTensor(), download=True)\n",
    "test_dataset = torchvision.datasets.CIFAR10('./CIFAR10_dataset', train=False,\n",
    "                                            transform=torchvision.transforms.ToTensor(), download=True)\n",
    "train_dataset_len = len(train_dataset)\n",
    "test_dataset_len = len(test_dataset)\n",
    "print(f'训练集的长度为：{train_dataset_len}')\n",
    "print(f'测试集的长度为：{test_dataset_len}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 利用dataloader加载数据\n",
    "train_dataset_loader = DataLoader(train_dataset, batch_size=64)\n",
    "test_dataset_loader = DataLoader(test_dataset, batch_size=64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Test(\n",
       "  (model1): Sequential(\n",
       "    (0): Conv2d(3, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))\n",
       "    (1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
       "    (2): Conv2d(32, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))\n",
       "    (3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
       "    (4): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))\n",
       "    (5): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
       "    (6): Flatten(start_dim=1, end_dim=-1)\n",
       "    (7): Linear(in_features=1024, out_features=64, bias=True)\n",
       "    (8): Linear(in_features=64, out_features=10, bias=True)\n",
       "  )\n",
       ")"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建网络模型\n",
    "device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n",
    "# device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
    "test = Test()\n",
    "# if torch.cuda.is_available():\n",
    "#     test.cuda()\n",
    "test.to(device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CrossEntropyLoss()"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 损失函数\n",
    "loss_fn = torch.nn.CrossEntropyLoss()\n",
    "# if torch.cuda.is_available():\n",
    "#     loss_fn.cuda()\n",
    "loss_fn.to(device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 优化器\n",
    "learning_rate = 0.01\n",
    "optimizer = torch.optim.SGD(test.parameters(), lr=learning_rate)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置训练网络的一些参数\n",
    "# 记录训练的次数\n",
    "total_train_step = 0\n",
    "# 记录测试的次数\n",
    "total_test_step = 0\n",
    "# 训练的轮数\n",
    "epoch = 25"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------第1轮训练开始----------------\n",
      "第100次训练，loss为2.2848832607269287\n",
      "第200次训练，loss为2.2747642993927\n",
      "第300次训练，loss为2.245779275894165\n",
      "第400次训练，loss为2.1397078037261963\n",
      "第500次训练，loss为2.028465509414673\n",
      "第600次训练，loss为2.0049655437469482\n",
      "第700次训练，loss为1.9759564399719238\n",
      "整体测试集上的Loss为310.2312927246094\n",
      "整体测试集上的正确率为0.28689998388290405\n",
      "第1轮花费时间为14.57308840751648\n",
      "----------------第1轮训练结束----------------\n",
      "----------------第2轮训练开始----------------\n",
      "第800次训练，loss为1.8433420658111572\n",
      "第900次训练，loss为1.8069300651550293\n",
      "第1000次训练，loss为1.862713098526001\n",
      "第1100次训练，loss为1.9722716808319092\n",
      "第1200次训练，loss为1.6687873601913452\n",
      "第1300次训练，loss为1.6240465641021729\n",
      "第1400次训练，loss为1.693163514137268\n",
      "第1500次训练，loss为1.755441665649414\n",
      "整体测试集上的Loss为299.71942138671875\n",
      "整体测试集上的正确率为0.3203999996185303\n",
      "第2轮花费时间为13.215519666671753\n",
      "----------------第2轮训练结束----------------\n",
      "----------------第3轮训练开始----------------\n",
      "第1600次训练，loss为1.67899751663208\n",
      "第1700次训练，loss为1.6405577659606934\n",
      "第1800次训练，loss为1.906273603439331\n",
      "第1900次训练，loss为1.6785932779312134\n",
      "第2000次训练，loss为1.9391074180603027\n",
      "第2100次训练，loss为1.5006591081619263\n",
      "第2200次训练，loss为1.4309272766113281\n",
      "第2300次训练，loss为1.7592289447784424\n",
      "整体测试集上的Loss为258.9541015625\n",
      "整体测试集上的正确率为0.40359997749328613\n",
      "第3轮花费时间为13.333105087280273\n",
      "----------------第3轮训练结束----------------\n",
      "----------------第4轮训练开始----------------\n",
      "第2400次训练，loss为1.7108111381530762\n",
      "第2500次训练，loss为1.3208576440811157\n",
      "第2600次训练，loss为1.5930556058883667\n",
      "第2700次训练，loss为1.6547393798828125\n",
      "第2800次训练，loss为1.4822502136230469\n",
      "第2900次训练，loss为1.5531606674194336\n",
      "第3000次训练，loss为1.330180287361145\n",
      "第3100次训练，loss为1.497488021850586\n",
      "整体测试集上的Loss为248.2795867919922\n",
      "整体测试集上的正确率为0.42399999499320984\n",
      "第4轮花费时间为17.586050271987915\n",
      "----------------第4轮训练结束----------------\n",
      "----------------第5轮训练开始----------------\n",
      "第3200次训练，loss为1.2947696447372437\n",
      "第3300次训练，loss为1.460748314857483\n",
      "第3400次训练，loss为1.4691466093063354\n",
      "第3500次训练，loss为1.5554946660995483\n",
      "第3600次训练，loss为1.5429105758666992\n",
      "第3700次训练，loss为1.3739941120147705\n",
      "第3800次训练，loss为1.30753755569458\n",
      "第3900次训练，loss为1.4558820724487305\n",
      "整体测试集上的Loss为244.507080078125\n",
      "整体测试集上的正确率为0.4302999973297119\n",
      "第5轮花费时间为16.694441556930542\n",
      "----------------第5轮训练结束----------------\n",
      "----------------第6轮训练开始----------------\n",
      "第4000次训练，loss为1.4163917303085327\n",
      "第4100次训练，loss为1.4277318716049194\n",
      "第4200次训练，loss为1.4675419330596924\n",
      "第4300次训练，loss为1.2163794040679932\n",
      "第4400次训练，loss为1.1427000761032104\n",
      "第4500次训练，loss为1.3348013162612915\n",
      "第4600次训练，loss为1.4077080488204956\n",
      "整体测试集上的Loss为237.18902587890625\n",
      "整体测试集上的正确率为0.45170000195503235\n",
      "第6轮花费时间为13.414499282836914\n",
      "----------------第6轮训练结束----------------\n",
      "----------------第7轮训练开始----------------\n",
      "第4700次训练，loss为1.332208275794983\n",
      "第4800次训练，loss为1.5063585042953491\n",
      "第4900次训练，loss为1.3153191804885864\n",
      "第5000次训练，loss为1.4222229719161987\n",
      "第5100次训练，loss为0.9917423725128174\n",
      "第5200次训练，loss为1.285239815711975\n",
      "第5300次训练，loss为1.2299576997756958\n",
      "第5400次训练，loss为1.39175546169281\n",
      "整体测试集上的Loss为226.92372131347656\n",
      "整体测试集上的正确率为0.4797999858856201\n",
      "第7轮花费时间为14.064949035644531\n",
      "----------------第7轮训练结束----------------\n",
      "----------------第8轮训练开始----------------\n",
      "第5500次训练，loss为1.1932178735733032\n",
      "第5600次训练，loss为1.2021987438201904\n",
      "第5700次训练，loss为1.1813148260116577\n",
      "第5800次训练，loss为1.2312023639678955\n",
      "第5900次训练，loss为1.3664878606796265\n",
      "第6000次训练，loss为1.5509247779846191\n",
      "第6100次训练，loss为1.052237629890442\n",
      "第6200次训练，loss为1.1428711414337158\n",
      "整体测试集上的Loss为216.36947631835938\n",
      "整体测试集上的正确率为0.5090999603271484\n",
      "第8轮花费时间为14.231741428375244\n",
      "----------------第8轮训练结束----------------\n",
      "----------------第9轮训练开始----------------\n",
      "第6300次训练，loss为1.4099642038345337\n",
      "第6400次训练，loss为1.1773380041122437\n",
      "第6500次训练，loss为1.5934984683990479\n",
      "第6600次训练，loss为1.1569157838821411\n",
      "第6700次训练，loss为1.0447193384170532\n",
      "第6800次训练，loss为1.1562005281448364\n",
      "第6900次训练，loss为1.0882835388183594\n",
      "第7000次训练，loss为0.9544420838356018\n",
      "整体测试集上的Loss为207.08351135253906\n",
      "整体测试集上的正确率为0.5342000126838684\n",
      "第9轮花费时间为17.044073343276978\n",
      "----------------第9轮训练结束----------------\n",
      "----------------第10轮训练开始----------------\n",
      "第7100次训练，loss为1.273861050605774\n",
      "第7200次训练，loss为0.9718976020812988\n",
      "第7300次训练，loss为1.0829533338546753\n",
      "第7400次训练，loss为0.8587080836296082\n",
      "第7500次训练，loss为1.2057982683181763\n",
      "第7600次训练，loss为1.2457101345062256\n",
      "第7700次训练，loss为0.8602688312530518\n",
      "第7800次训练，loss为1.2344111204147339\n",
      "整体测试集上的Loss为199.59234619140625\n",
      "整体测试集上的正确率为0.5518999695777893\n",
      "第10轮花费时间为15.410892248153687\n",
      "----------------第10轮训练结束----------------\n",
      "----------------第11轮训练开始----------------\n",
      "第7900次训练，loss为1.3232977390289307\n",
      "第8000次训练，loss为1.1280626058578491\n",
      "第8100次训练，loss为0.9926409721374512\n",
      "第8200次训练，loss为1.2621612548828125\n",
      "第8300次训练，loss为1.2633051872253418\n",
      "第8400次训练，loss为1.1305606365203857\n",
      "第8500次训练，loss为1.121592402458191\n",
      "第8600次训练，loss为0.8907811045646667\n",
      "整体测试集上的Loss为192.87832641601562\n",
      "整体测试集上的正确率为0.567799985408783\n",
      "第11轮花费时间为16.587311029434204\n",
      "----------------第11轮训练结束----------------\n",
      "----------------第12轮训练开始----------------\n",
      "第8700次训练，loss为1.179368495941162\n",
      "第8800次训练，loss为1.39730966091156\n",
      "第8900次训练，loss为1.0645427703857422\n",
      "第9000次训练，loss为1.1378026008605957\n",
      "第9100次训练，loss为1.096633791923523\n",
      "第9200次训练，loss为1.0338038206100464\n",
      "第9300次训练，loss为1.0515586137771606\n",
      "整体测试集上的Loss为187.78282165527344\n",
      "整体测试集上的正确率为0.5780999660491943\n",
      "第12轮花费时间为13.910033464431763\n",
      "----------------第12轮训练结束----------------\n",
      "----------------第13轮训练开始----------------\n",
      "第9400次训练，loss为0.8858600854873657\n",
      "第9500次训练，loss为1.2909196615219116\n",
      "第9600次训练，loss为1.2191598415374756\n",
      "第9700次训练，loss为1.0935217142105103\n",
      "第9800次训练，loss为0.9852256178855896\n",
      "第9900次训练，loss为0.9829613566398621\n",
      "第10000次训练，loss为0.9532082676887512\n",
      "第10100次训练，loss为0.9513511657714844\n",
      "整体测试集上的Loss为183.57827758789062\n",
      "整体测试集上的正确率为0.5895000100135803\n",
      "第13轮花费时间为14.43833303451538\n",
      "----------------第13轮训练结束----------------\n",
      "----------------第14轮训练开始----------------\n",
      "第10200次训练，loss为0.7967581152915955\n",
      "第10300次训练，loss为0.9816645979881287\n",
      "第10400次训练，loss为1.222029685974121\n",
      "第10500次训练，loss为0.8104259967803955\n",
      "第10600次训练，loss为0.9495944976806641\n",
      "第10700次训练，loss为0.8454057574272156\n",
      "第10800次训练，loss为0.8996037840843201\n",
      "第10900次训练，loss为1.0029926300048828\n",
      "整体测试集上的Loss为180.0153045654297\n",
      "整体测试集上的正确率为0.5974000096321106\n",
      "第14轮花费时间为14.086106061935425\n",
      "----------------第14轮训练结束----------------\n",
      "----------------第15轮训练开始----------------\n",
      "第11000次训练，loss为1.2006142139434814\n",
      "第11100次训练，loss为0.8143194913864136\n",
      "第11200次训练，loss为0.9881386160850525\n",
      "第11300次训练，loss为1.153823733329773\n",
      "第11400次训练，loss为0.7571778297424316\n",
      "第11500次训练，loss为1.1170969009399414\n",
      "第11600次训练，loss为0.9413381814956665\n",
      "第11700次训练，loss为0.9903512597084045\n",
      "整体测试集上的Loss为176.32447814941406\n",
      "整体测试集上的正确率为0.6080999970436096\n",
      "第15轮花费时间为16.65819549560547\n",
      "----------------第15轮训练结束----------------\n",
      "----------------第16轮训练开始----------------\n",
      "第11800次训练，loss为0.8842920064926147\n",
      "第11900次训练，loss为0.8952651619911194\n",
      "第12000次训练，loss为0.8420328497886658\n",
      "第12100次训练，loss为0.9278349280357361\n",
      "第12200次训练，loss为0.9479148387908936\n",
      "第12300次训练，loss为0.844163179397583\n",
      "第12400次训练，loss为0.8906272649765015\n",
      "第12500次训练，loss为0.7081614136695862\n",
      "整体测试集上的Loss为173.92874145507812\n",
      "整体测试集上的正确率为0.6152999997138977\n",
      "第16轮花费时间为15.100520372390747\n",
      "----------------第16轮训练结束----------------\n",
      "----------------第17轮训练开始----------------\n",
      "第12600次训练，loss为0.8127378225326538\n",
      "第12700次训练，loss为0.8602153658866882\n",
      "第12800次训练，loss为0.7935370206832886\n",
      "第12900次训练，loss为1.1219518184661865\n",
      "第13000次训练，loss为1.0045911073684692\n",
      "第13100次训练，loss为0.6602407097816467\n",
      "第13200次训练，loss为0.7362091541290283\n",
      "整体测试集上的Loss为171.56396484375\n",
      "整体测试集上的正确率为0.6222999691963196\n",
      "第17轮花费时间为15.038898468017578\n",
      "----------------第17轮训练结束----------------\n",
      "----------------第18轮训练开始----------------\n",
      "第13300次训练，loss为0.9562942385673523\n",
      "第13400次训练，loss为0.7784746885299683\n",
      "第13500次训练，loss为0.8593526482582092\n",
      "第13600次训练，loss为1.2863671779632568\n",
      "第13700次训练，loss为0.7972199320793152\n",
      "第13800次训练，loss为1.01344633102417\n",
      "第13900次训练，loss为0.6419727802276611\n",
      "第14000次训练，loss为0.7011740803718567\n",
      "整体测试集上的Loss为169.91958618164062\n",
      "整体测试集上的正确率为0.6283000111579895\n",
      "第18轮花费时间为15.01124382019043\n",
      "----------------第18轮训练结束----------------\n",
      "----------------第19轮训练开始----------------\n",
      "第14100次训练，loss为0.9955759644508362\n",
      "第14200次训练，loss为0.8400796055793762\n",
      "第14300次训练，loss为0.8862594366073608\n",
      "第14400次训练，loss为0.8868619203567505\n",
      "第14500次训练，loss为0.9253047704696655\n",
      "第14600次训练，loss为1.0266727209091187\n",
      "第14700次训练，loss为0.7319000363349915\n",
      "第14800次训练，loss为1.1570930480957031\n",
      "整体测试集上的Loss为168.1673583984375\n",
      "整体测试集上的正确率为0.6340999603271484\n",
      "第19轮花费时间为15.008969068527222\n",
      "----------------第19轮训练结束----------------\n",
      "----------------第20轮训练开始----------------\n",
      "第14900次训练，loss为0.5695327520370483\n",
      "第15000次训练，loss为0.8448001742362976\n",
      "第15100次训练，loss为0.805831789970398\n",
      "第15200次训练，loss为0.7979801893234253\n",
      "第15300次训练，loss为0.6850408315658569\n",
      "第15400次训练，loss为0.8219077587127686\n",
      "第15500次训练，loss为0.920021653175354\n",
      "第15600次训练，loss为0.8570014238357544\n",
      "整体测试集上的Loss为167.13412475585938\n",
      "整体测试集上的正确率为0.6395999789237976\n",
      "第20轮花费时间为15.038723707199097\n",
      "----------------第20轮训练结束----------------\n",
      "----------------第21轮训练开始----------------\n",
      "第15700次训练，loss为0.7352706789970398\n",
      "第15800次训练，loss为0.9056398272514343\n",
      "第15900次训练，loss为0.9564394354820251\n",
      "第16000次训练，loss为0.8301208019256592\n",
      "第16100次训练，loss为0.690002977848053\n",
      "第16200次训练，loss为0.8211737275123596\n",
      "第16300次训练，loss为0.7299969792366028\n",
      "第16400次训练，loss为0.7787091135978699\n",
      "整体测试集上的Loss为166.02577209472656\n",
      "整体测试集上的正确率为0.6419999599456787\n",
      "第21轮花费时间为14.882153987884521\n",
      "----------------第21轮训练结束----------------\n",
      "----------------第22轮训练开始----------------\n",
      "第16500次训练，loss为0.9297865033149719\n",
      "第16600次训练，loss为0.8045567274093628\n",
      "第16700次训练，loss为0.8937208652496338\n",
      "第16800次训练，loss为0.7910129427909851\n",
      "第16900次训练，loss为0.6269034743309021\n",
      "第17000次训练，loss为1.2197325229644775\n",
      "第17100次训练，loss为0.6389418244361877\n",
      "第17200次训练，loss为0.6320127844810486\n",
      "整体测试集上的Loss为165.23678588867188\n",
      "整体测试集上的正确率为0.6451999545097351\n",
      "第22轮花费时间为15.335750818252563\n",
      "----------------第22轮训练结束----------------\n",
      "----------------第23轮训练开始----------------\n",
      "第17300次训练，loss为0.9614038467407227\n",
      "第17400次训练，loss为0.9264383316040039\n",
      "第17500次训练，loss为0.6200127601623535\n",
      "第17600次训练，loss为0.7373647689819336\n",
      "第17700次训练，loss为0.9368211030960083\n",
      "第17800次训练，loss为0.8105145692825317\n",
      "第17900次训练，loss为0.7869799137115479\n",
      "整体测试集上的Loss为165.85850524902344\n",
      "整体测试集上的正确率为0.6462000012397766\n",
      "第23轮花费时间为17.30899667739868\n",
      "----------------第23轮训练结束----------------\n",
      "----------------第24轮训练开始----------------\n",
      "第18000次训练，loss为0.8191758990287781\n",
      "第18100次训练，loss为0.557363748550415\n",
      "第18200次训练，loss为0.8869115114212036\n",
      "第18300次训练，loss为0.6129292845726013\n",
      "第18400次训练，loss为0.5860546231269836\n",
      "第18500次训练，loss为0.7689790725708008\n",
      "第18600次训练，loss为0.7408720254898071\n",
      "第18700次训练，loss为0.7432721853256226\n",
      "整体测试集上的Loss为165.24176025390625\n",
      "整体测试集上的正确率为0.651699960231781\n",
      "第24轮花费时间为15.68037748336792\n",
      "----------------第24轮训练结束----------------\n",
      "----------------第25轮训练开始----------------\n",
      "第18800次训练，loss为0.8076490163803101\n",
      "第18900次训练，loss为0.5099862217903137\n",
      "第19000次训练，loss为0.6137213706970215\n",
      "第19100次训练，loss为0.634364902973175\n",
      "第19200次训练，loss为0.49041110277175903\n",
      "第19300次训练，loss为0.7137761116027832\n",
      "第19400次训练，loss为0.8380950093269348\n",
      "第19500次训练，loss为0.5788594484329224\n",
      "整体测试集上的Loss为165.6770782470703\n",
      "整体测试集上的正确率为0.6520999670028687\n",
      "第25轮花费时间为18.35328960418701\n",
      "----------------第25轮训练结束----------------\n"
     ]
    }
   ],
   "source": [
    "for i in range(epoch):\n",
    "    print(f'----------------第{i + 1}轮训练开始----------------')\n",
    "    start_time = time.time()\n",
    "    for data in train_dataset_loader:\n",
    "        imgs, target = data\n",
    "        # if torch.cuda.is_available():\n",
    "        #     imgs = imgs.cuda()\n",
    "        #     target = target.cuda()\n",
    "        imgs = imgs.to(device)\n",
    "        target = target.to(device)\n",
    "        output = test(imgs)\n",
    "        loss = loss_fn(output, target)\n",
    "\n",
    "        #  优化器优化\n",
    "        optimizer.zero_grad()\n",
    "        loss.backward()\n",
    "        optimizer.step()\n",
    "\n",
    "        total_train_step += 1\n",
    "        if total_train_step % 100 == 0:\n",
    "            print(f'第{total_train_step}次训练，loss为{loss.item()}')\n",
    "\n",
    "    total_test_loss = 0\n",
    "    total_accuracy = 0\n",
    "    with torch.no_grad():\n",
    "        for data in test_dataset_loader:\n",
    "            imgs, target = data\n",
    "            # if torch.cuda.is_available():\n",
    "            #     imgs = imgs.cuda()\n",
    "            #     target = target.cuda()\n",
    "            imgs = imgs.to(device)\n",
    "            target = target.to(device)\n",
    "            output = test(imgs)\n",
    "            loss = loss_fn(output, target)\n",
    "            total_test_loss += loss\n",
    "            accuracy = (output.argmax(1) == target).sum()\n",
    "            total_accuracy += accuracy\n",
    "        print(f'整体测试集上的Loss为{total_test_loss}')\n",
    "        print(f'整体测试集上的正确率为{total_accuracy / test_dataset_len}')\n",
    "    print(f'第{i + 1}轮花费时间为{time.time() - start_time}')\n",
    "    print(f'----------------第{i + 1}轮训练结束----------------')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python_study",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
